package com.netjava.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.netjava.pojo.Delivery;
import com.netjava.service.IDeliveryService;
import com.netjava.service.IUserService;
import com.netjava.service.impl.DeliveryService;


public class DeliveryAction extends BaseAction{
	public ActionForward getByDeliverState(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)  {
		ActionForward forward=mapping.getInputForward();
		IDeliveryService service=(IDeliveryService)getBean("deliveryService");
		Integer deliverState=Integer.parseInt(request.getParameter("state").toString());
		List<Delivery> deliveriedList=service.getByDeliverState(deliverState);

		if(deliverState==0){
			request.getSession().setAttribute("deliveriedList", deliveriedList);
			
		}
		else{
			request.getSession().setAttribute("deliveriedList", deliveriedList);
		}
		request.getSession().setAttribute("deliveriedList", deliveriedList);
		request.setAttribute("state", deliverState);
		
		forward=mapping.findForward("delivery");
		return forward;
	}
	public ActionForward modifyDelivery(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		Integer id=Integer.parseInt(request.getParameter("deliveryId").toString());
		IDeliveryService deliveryService=(IDeliveryService)getBean("deliveryService");
		//根据配送商品的状态
		Delivery delivery = deliveryService.getDeliveryById(id);
		if(null!=delivery){
			delivery.setDeliverState(1);
			deliveryService.attachDirty(delivery);
			//request.getSession().setAttribute("deliveriedList", delivery);
			request.setAttribute("msg", "发送成功！");
			request.setAttribute("state", 0);
		}
//		ActionForward forward=mapping.getInputForward();
//		forward=mapping.findForward("delivery");
		return new ActionForward("/delivery.do?operation=getByDeliverState&state=0");
	}
	public ActionForward deleteDelivery(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		Integer id=Integer.parseInt(request.getParameter("deliveryId").toString());
		IDeliveryService deliveryService=(IDeliveryService)getBean("deliveryService");
		//根据配送商品的状态
		Delivery delivery = deliveryService.getDeliveryById(id);
		if(null!=delivery){
			deliveryService.delete(delivery);
			request.setAttribute("msg", "删除成功！");
		}

		return new ActionForward("/delivery.do?operation=getByDeliverState&state=1");
	}
}
